<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Validator;

class VipController extends Controller {
	/*
		     * @会员管理的首页
	*/
	public function index(Request $request) {
		if ($request->isMethod('post')) {
			$name = $request->input('username');
			$paginate = 15;
			$vips = User::where('name', 'like', '%' . $name . '%')->paginate($paginate);
			//带条件的有参数的统计
			$count = User::where('name', 'like', '%' . $name . '%')->count();
			$total_page = ceil($count / $paginate); //共多少页
			//        dd($vips);
			return view("admin.vip", ['name' => $name])
				->with('vips', $vips)
				->with('total_page', $total_page);
		}
		/*=======get的传递方式==========*/
		//注意参数post请求表单的username,  get请求post的name
		$name = $request->input('name');
		$paginate = 15;
		$vips = User::where('name', 'like', '%' . $name . '%')->paginate($paginate);
		//带条件的有参数的统计
		$count = User::where('name', 'like', '%' . $name . '%')->count();
		$total_page = ceil($count / $paginate); //共多少页v
		//        dd($vips);
		return view("admin.vip", ['name' => $name])
			->with('vips', $vips)
			->with('total_page', $total_page);

	}
	/*
		     * @查看会员详情与编辑用户基本信息
	*/
	public function show($id) {
		$masters = User::find($id);
		$paginate = 5;
		$orders = DB::table('orders')->join('users', 'users.id', '=', 'orders.user_id')
			->join('shops', 'shops.id', '=', 'orders.shop_id')
			->select('orders.*', 'shops.shop_name')
			->where('users.id', '=', $id)
			->paginate($paginate);
//        dd($orders);
		$count = DB::table('orders')->join('users', 'users.id', '=', 'orders.user_id')
			->join('shops', 'shops.id', '=', 'orders.shop_id')
			->select('orders.*', 'shops.shop_name')
			->where('users.id', '=', $id)->count();
		$total_page = ceil($count / $paginate); //共多少页v
		return view("admin.vip_show")
			->with('orders', $orders)
			->with('masters', $masters)->with('total_page', $total_page);
	}

	/*
		     * @会员相关信息编辑修改
	*/
	public function update(Request $request, $id) {
		$vips = User::find($id);
		if ($request->isMethod('post')) {
			$roles = [
				'name' => 'required|max:5',
				'age' => 'required|numeric',
				'id_card' => 'required|identitycards',
				'telphone' => 'required|telphone',
			];
            $messages = [
				'name.required' => '请填写姓名',
				'name.max' => '姓名格式有误',
				'age.required' => '请填写年龄',
				'age.numeric' => '年龄格式有误',
				'id_card.required' => '请填写身份证号码',
				'id_card.identitycards' => '身份证号码格式有误',
				'telphone.required' => '请填写手机号码',
				'telphone.telphone' => '手机号码格式有误',

			];

			$validator = \Validator::make($request->all(), $roles, $messages);
			if ($validator->fails()) {
                $error = $validator->errors()->all();
                return ['state' => 0, 'msg' => $error[0]];
			}
			$name = $request->get('name');
			$age = $request->get('age');
			$sex = $request->get('sex');
			$telphone = $request->get('telphone');
			$id_card = $request->get('id_card');
//            排除自己的查找,根据id,验证规则也去除
			$res = User::where('telphone', '!=', $vips->telphone)->where('telphone', $telphone)->first();
            if ($res) {
                return ['state' =>0, 'msg' =>'该手机号已经存在'];
            }
            $reult = User::where('id_card', '!=', $vips->id_card)->where('id_card', $id_card)->first();
            if ($reult) {
                return ['state' =>0, 'msg' =>'该身份证号已经存在'];
            }

            $vips->id_card = $id_card;
            $vips->telphone = $telphone;
			$vips->name = $name;
			$vips->age = $age;
			$vips->sex = $sex;
			$vips->save();
            return ['state' => 1, 'msg' =>'添加成功'];

		}
	}
	/*
		      * @是否禁用用户
	*/
	public function status(Request $request) {

		$is_show = $request->is_show;
		$id = $request->id;

		if ($is_show == 1) {
			$is_show = 0;
		} else {
			$is_show = 1;
		}
		$res = User::find($id)
			->update(['status' => $is_show]);
		return $is_show;

	}
}
